using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;
using smartBrowser.GUI;
using System.Net;
using System.Windows.Forms;
using smartBrowser.Util;
using System.Text.RegularExpressions;

namespace smartBrowser.Util
{
    class SearchObject
    {
        private string query;
        private int max_results;
        

        public SearchObject(String query, int max_results)
        {
            this.max_results = max_results;
            this.query = query;
        }


        public List<Result> searchResults()
        {
            string contents = WebPageFetcher.FecthPage(new Uri("http://www.google.pt/search?q=" + query.Trim().Replace(' ', '+')));

            String expression = "<h2 class=r><a class=l href=\"(.*?)\".*?>(.*?)</a>";
            Regex regex = new Regex(expression);
            MatchCollection matches = regex.Matches(contents);

            List<Result> links = new List<Result>();
            foreach (Match match in matches)
            {
                Result result = new Result(match.Groups[2].Value, match.Groups[1].Value);
                links.Add(result);
            }

            return links;
        }
    }
}
